Abstract Class
The abstract is a keyword in Java and you can declare Java class as abstract. An abstract class may have both abstract and non-abstract methods. Abstraction allows you to hide the program details and shows the program functionality to the users. For example- calling over the phone, you dial the number but do not know how the call process works. It allows you to focus on what the object does rather than focusing on how the object does it.
You can achieve abstraction by- abstract class and interfaces.
You have to extend the abstract class in order to provide the definition of its data members. There is no need to create an instance of the abstract class.
Features of Abstract class-
- It has to be declared with the abstract keyword.
- You can declare abstract and non-abstract methods within the abstract class.
- No need to create the instance of an abstract class.
- You can declare constructor and static members.
- You can even have final methods in this class.
Syntax-
Abstract class demo{}
Abstract Method-
The methods that are declared with abstract keyword and we do not provide the body definition for that method. The definition is provided in the class that extends the abstract class including the abstract method.
Abstract void display();
Example-
abstract class demo{
abstract void display();}
class Simple extends demo{
void display(){
System.out.println("welcome");}
public static void main(String args[]){
Simple d= new Simple();
d.display();
}
}
Output-
welcome
Example with constructor-
abstract class demo{
demo()
{System.out.println("hello");}
abstract void display();}
class Simple extends demo{
void display(){
System.out.println("welcome");}
public static void main(String args[]){
Simple d= new Simple();
d.display();
}
}
Output-
hello
welcome